package me.ezitku.base.extensions.Contancts

import android.content.Context
import android.provider.CallLog
import android.provider.ContactsContract
import android.provider.Telephony
import me.ezitku.base.extensions.Contancts.model.CallLogs
import me.ezitku.base.extensions.Contancts.model.ContactBean
import me.ezitku.base.extensions.Contancts.model.SmsBean
import me.ezitku.base.extensions.longToDate
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList


/**
 * قىسقا ئۇچۇر ، ئالاقىداش  ، سۆزلىشىش خاتىرىسى
 */

/**
 * بارلىق سۆزلىشىش خاتىرىسى
 * (通话记录)
 */
private typealias Call = CallLog.Calls
val Context.CallLogs: ArrayList<CallLogs>
    get() {
        var aa = arrayOf(
                Call.CACHED_NAME// 通话记录的联系人
                , Call.NUMBER// 通话记录的电话号码
                , Call.DATE// 通话记录的日期
                , Call.DURATION// 通话时长
                , Call.TYPE // 通话类型
                , null, null, CallLog.Calls.DEFAULT_SORT_ORDER// 按照时间逆序排列，最近打的最先显示
        )
//        var cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, ConstraintLayout, null, null, CallLog.Calls.DEFAULT_SORT_ORDER)
        var cursor = contentResolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null)
        var nameIdx = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME)
        var numberIdx = cursor.getColumnIndex(CallLog.Calls.NUMBER)
        var dateIdx = cursor.getColumnIndex(CallLog.Calls.DATE)
        var durationIdx = cursor.getColumnIndex(CallLog.Calls.DURATION)
        var typeIdx = cursor.getColumnIndex(CallLog.Calls.TYPE)

        val callLogs = ArrayList<CallLogs>()
        while (cursor.moveToNext()) {
            var calllog = CallLogs()
            calllog.name = cursor.getString(nameIdx) ?: "未备注联系人"
            calllog.date = SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(Date(cursor.getLong(dateIdx)))
            calllog.number = cursor.getString(numberIdx)
            calllog.duration = (cursor.getInt(durationIdx) / 60).toString() + "分钟"
            val type = cursor.getInt(typeIdx)
            var typeString = ""
            when (type) {
                CallLog.Calls.INCOMING_TYPE -> typeString = "打入"
                CallLog.Calls.OUTGOING_TYPE -> typeString = "打出"
                CallLog.Calls.MISSED_TYPE -> typeString = "未接"
                else -> {
                }
            }
            calllog.type = typeString
            callLogs.add(calllog)
        }


        return callLogs
    }


/**
 *بارلىق قىسقا ئۇچۇر
 */
val Context.Smses: ArrayList<SmsBean>
    get() {
        val projection = arrayOf("_id", Telephony.Sms.ADDRESS, Telephony.Sms.PERSON,
                Telephony.Sms.BODY, Telephony.Sms.DATE, Telephony.Sms.TYPE)
        val cusor = contentResolver.query(Telephony.Sms.CONTENT_URI, projection, null, null, "date desc")

        val nameColumn = cusor.getColumnIndex("person")
        val phoneNumberColumn = cusor.getColumnIndex("address")
        val smsbodyColumn = cusor.getColumnIndex("body")
        val dateColumn = cusor.getColumnIndex("date")
        val typeColumn = cusor.getColumnIndex("type")
        var datas = ArrayList<SmsBean>()
        if (cusor != null) {
            while (cusor.moveToNext()) {
                val smsinfo = SmsBean()
                smsinfo.name = cusor.getString(nameColumn)
                smsinfo.date = cusor.getString(dateColumn).longToDate()
                smsinfo.number = cusor.getString(phoneNumberColumn)
                smsinfo.body = cusor.getString(smsbodyColumn)
                var typeI = cusor.getInt(typeColumn)
                smsinfo.type = when (typeI) { 1 -> "接收";2 -> "发送"; else -> ""
                }
                datas.add(smsinfo)
            }
            cusor.close()
        }
        return datas
    }


/**
 *بارلىق ئالاقىداش
 */
val Context.Contacts: ArrayList<ContactBean>
    get() {
        val cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null)
        val idxName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
        val idxPhoneNum = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
        val idxID = cursor.getColumnIndex(ContactsContract.Contacts._ID)

        var datas = ArrayList<ContactBean>()
        if (cursor != null) {
            while (cursor.moveToNext()) {
                var contact = ContactBean()
                contact.name = cursor.getString(idxName)
                contact.number = cursor.getString(idxPhoneNum)
                datas.add(contact)
            }
        }
        cursor.close()
        return datas
    }
